/**
 * Created by zhangzhihao on 2016/12/06 11:00;
 */
var vm = new Vue({
	el: '#main',
	data: {
		adduser: {
			deptNo:"",
			deptName:""
		},
        branchTree: [],
        zTree: {},
        setting: {}	,
        optTypes:[]
	},
	created:function(){
		this.queryUserType();
		this.setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "branchId",
                    pIdKey: "branchParentId"
                },
                key: {
                    name: "branchName"
                }
            },
            async: {    
                enable: true,    
                url:App.getApiUrl("getUserBranchURL"),    
                autoParam:["branchId"] 
            },  
            callback: {
				onClick: this.branchClick,
				onAsyncSuccess: this.zTreeOnAsyncSuccess,//异步加载成功的fun    
				onAsyncError: this.zTreeOnAsyncError   //加载错误的fun    
			}
        };
    	this.getBranchTree();
    	
    	setTimeout(function(){
			$('#optType').on('change', function() {
				vm.adduser.optType = $(this).val();
			});
		}, 1);
	},
	methods: {
		//返回上一页的点击事件的函数
		goBtn: function() {
			//不保留
			history.back(-1);
			//保留
			//			history.go(-1);
		},
		 queryUserType:function(){
	        	App.ajax({
	    			type:"GET",
	    			url:App.getApiUrl("queryUserTypeURL"),
	    			success:function(data){
	    				vm.optTypes = data;
					}
				});
	        },
		getBranchTree:function(){
			App.ajax({
				type:"GET",
				url:App.getApiUrl("getUserBranchURL"),
				success:function(data){                
					vm.branchTree = data;
                    $.fn.zTree.init($('#branchTree', parent.document) , vm.setting, vm.branchTree);
                    vm.zTree = $.fn.zTree.getZTreeObj("branchTree");
				}
			});
        },
		branchClick:function(evevt, treeId, treeNode){
			parent.closeConfirm('my-confirmOne');
			vm.adduser.deptNo = treeNode.branchId;
			vm.adduser.deptName = treeNode.branchName;
		},
		//获取焦点事件弹出机构树
		confirmTissue:function(){
//			console.log(12345)
//			parent.showConfirm(
//				
//			);
			parent.showConfirmOne(
				
			);
		},
		//保存按钮触发的事件
		saveBtn: function() {
			if(!this.adduser.deptNo) {
				parent.showAlert("组织机构不能为空！")
				return;
			}
			if(!this.adduser.optNo) {
				parent.showAlert("用户登录名不能为空！")
				return;
			}
			if(!this.adduser.optName) {
				parent.showAlert("用户姓名不能为空！")
				return;
			}
			if(!this.adduser.optType) {
				parent.showAlert("请选择用户类型！")
				return;
			}
			if(!this.adduser.optPassword || !this.adduser.optPasswordT) {
				parent.showAlert("密码或确认密码不能为空！")
				return;
			}
			if(this.adduser.optPassword!=this.adduser.optPasswordT) {
				parent.showAlert("两次密码不一致！")
				return;
			}
			
			App.ajax({
    			type:"POST",
    			url:App.getApiUrl("addUser"),
    			data:this.adduser,
    			success:function(data){
    				parent.showAlert("添加用户成功");
				}
			});
		},
		beforeExpand: function(treeId, treeNode) {
			if (!treeNode.isAjaxing) {
				return true;
			} else {
				parent.showAlert("正在下载数据中，请稍后展开节点。。。");
				return false;
			}
		},
		zTreeOnAsyncError: function (event, treeId, treeNode){    
            alert("异步加载失败!");    
        },   
		zTreeOnAsyncSuccess: function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){    
			 
			treeNode.icon = "";
			vm.zTree.updateNode(treeNode);
        }  
	}
});